{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "293d248e",
   "metadata": {},
   "source": [
    "-----\n",
    "* 本周主要内容：图像识别（人脸识别）\n",
    "* 23春_API_人工智能与机器学习_week05\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d590ca5",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "\n",
    "## 1. 人脸识别概念和基础\n",
    "## 2. 人脸识别应用\n",
    "## 3. 人脸识别实践-face++\n",
    "\n",
    "> 1. API文档阅读规范\n",
    "> 2. 人脸检测（人脸分析）实践\n",
    "> 3. 输入/输出\n",
    "> 4. 错误码寻找原因\n",
    "\n",
    "# 本周内容\n",
    "\n",
    "> 1. 人脸比对\n",
    "> 2. 人脸搜索\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef95d0b1",
   "metadata": {},
   "source": [
    "# 人脸比对\n",
    "![image.png](compare_app.jpg)\n",
    "\n",
    "\n",
    "## 原理和概念\n",
    "\n",
    "\n",
    "\n",
    "## 人脸对比实践\n",
    "\n",
    "* 将两个人脸进行比对，来判断是否为同一个人，返回比对结果置信度和不同误识率下的阈值。支持传入图片或 face_token 进行比对。使用图片时会自动选取图片中检测到人脸尺寸最大的一个人脸。\n",
    "![image.png](face_compare.jpg)\n",
    "\n",
    "## 人脸数据库介绍\n",
    "###  人脸数据存储规则\n",
    "### 人脸库管理\n",
    "> 1. 创建创建人脸库\n",
    "> 2. 获取人脸库信息 \n",
    "> 3. 添加人脸\n",
    "> 4. 获取人脸库信息\n",
    "\n",
    "\n",
    "## 人脸搜索\n",
    "![image.png](face_search.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24ce08c8",
   "metadata": {},
   "source": [
    "# 人脸比对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0b1be5ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679281595,4026fea7-7a68-4d10-a8c8-ae2f33bb2da9', 'time_used': 380, 'confidence': 81.858, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': '20b1dff9877cd5f778f1192d40d51f09', 'face_rectangle': {'top': 75, 'left': 173, 'width': 114, 'height': 114}}], 'faces2': [{'face_token': 'e8a6705c0fdcf2cdf53d23d020f4c5dc', 'face_rectangle': {'top': 87, 'left': 318, 'width': 131, 'height': 131}}], 'image_id1': 'qesNyjZJhrX0z/ru0Rao5A==', 'image_id2': 'W8NpWno8wrTKB2cFSF1mIg=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('gao_01.jpg', 'rb')\n",
    "f_02 = open('gao_02.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov\",\n",
    "    \"api_secret\":\"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\",\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5283743",
   "metadata": {},
   "source": [
    "# 2. 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45b7ab47",
   "metadata": {},
   "source": [
    "## 2.1 人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fe560be",
   "metadata": {},
   "source": [
    "### 2.1.1创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a46d877e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': '689f8e1cf460b1346a420f32deb2af15', 'time_used': 199, 'face_count': 0, 'face_added': 0, 'request_id': '1679283083,bec1d6dc-5be5-4a42-865d-523ab2659173', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov\",\n",
    "    \"api_secret\":\"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\",\n",
    "    \"display_name\":\"广州南方学院1栋宿舍楼\"\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19c8c59f",
   "metadata": {},
   "source": [
    "### 2.1.2 检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6d91aaa7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '689f8e1cf460b1346a420f32deb2af15',\n",
       " 'tags': '',\n",
       " 'time_used': 96,\n",
       " 'user_data': '',\n",
       " 'display_name': '广州南方学院1栋宿舍楼',\n",
       " 'face_tokens': ['7d2ef00e802300d8c6538511b4f2c684',\n",
       "  '675479a01a9a5779ae71ea8db08b189e',\n",
       "  '9d3219e623e96472d78a94de417ad77f'],\n",
       " 'face_count': 3,\n",
       " 'request_id': '1679284449,822e39e0-870d-4df2-9816-537a984311f6',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov\",\n",
    "    \"api_secret\":\"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\",\n",
    "    \"faceset_token\":\"689f8e1cf460b1346a420f32deb2af15\"\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c01a1ed5",
   "metadata": {},
   "source": [
    "### 2.1.3增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "44a81196",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679284104,7ec917d4-8380-4621-ae0d-42e9c6c05679', 'time_used': 38, 'faces': [{'face_token': '9d3219e623e96472d78a94de417ad77f', 'face_rectangle': {'top': 59, 'left': 189, 'width': 146, 'height': 146}}], 'image_id': 't1X3n531bX1gXG4adI88DA==', 'face_num': 1}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# 先检测人脸信息\n",
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测: 刘亦菲、蔡徐坤、高启强\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('1栋/gqq.jpg', 'rb')\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov\",\n",
    "    \"api_secret\":\"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\",\n",
    "    \"image_base64\":img,\n",
    "\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0da66a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ['7d2ef00e802300d8c6538511b4f2c684','675479a01a9a5779ae71ea8db08b189e','9d3219e623e96472d78a94de417ad77f']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "baf58b56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '689f8e1cf460b1346a420f32deb2af15',\n",
       " 'time_used': 496,\n",
       " 'face_count': 3,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1679284437,8e5697aa-196d-44e4-b545-c878facf4d07',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov\",\n",
    "    \"api_secret\":\"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\",\n",
    "    \"faceset_token\":\"689f8e1cf460b1346a420f32deb2af15\",\n",
    "    \"face_tokens\":'9d3219e623e96472d78a94de417ad77f'\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "601601d2",
   "metadata": {},
   "source": [
    "### 2.1.4检查人脸集合的人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d45a244",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "368.398px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
